package cn.kgc.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.kgc.entity.Goods;
import cn.kgc.mapper.GoodsMapper;
import cn.kgc.service.GoodsService;

@Controller
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping(path = "/goods",method = RequestMethod.GET)
	public String getAllGoods(Model model) {
		List<Goods> list=goodsService.getAllGoods();
		model.addAttribute("goodsList", list);
		return "goodsList";
	}
	
	@RequestMapping(path ="/goods/add",method = RequestMethod.GET)
	public String toGoodsUI(Model model) {
		model.addAttribute("goods", new Goods());
		return "goods";
	}
	
	@RequestMapping(path ="/goods",method = RequestMethod.POST)
	public String addGoods(Goods goods) {
		goodsService.addGoods(goods);
		return "redirect:/goods";
	}
	
	@RequestMapping(path ="/goods/{id}",method = RequestMethod.GET)
	public String getGoodsById(@PathVariable("id") Integer id,Model model) {
		Goods goods=goodsService.getGoodsById(id);
		model.addAttribute("goods", goods);
		return "goods";
	}
	
	@RequestMapping(path ="/goods",method = RequestMethod.PUT)
	public String updateGoods(Goods goods) {
		goodsService.updateGoods(goods);
		return "redirect:/goods";
	}
	
	@RequestMapping(path ="/goods/{id}",method = RequestMethod.DELETE)
	public String deleteGoodsById(@PathVariable("id") Integer id) {
		goodsService.deleteGoodsById(id);
		return "redirect:/goods";
	}
	
	
}
